<?php

/**
 * This is the model class for table "{{recoveryemail}}".
 *
 * The followings are the available columns in table '{{recoveryemail}}':
 * @property integer $id
 * @property integer $user_id
 * @property string $uniqid
 * @property integer $create_time
 * @property integer $exp_time
 */
class RecoveryEmail extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return RecoveryEmail the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{recoveryemail}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, uniqid, create_time, exp_time', 'required'),
            array('user_id, create_time, exp_time', 'numerical', 'integerOnly' => true),
            array('uniqid', 'length', 'max' => 32),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, user_id, uniqid, create_time, exp_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * create random unique id, create time and exp time (7 days)
     * @return boolean
     */
    public function beforeSave() {
        if(parent::beforeSave()){
            //generate uniqid
            $this->uniqid = uniqid();
            $this->create_time = time();
            $this->exp_time = strtotime('+1 week');

            return true;
        }

        return false;
    }
}